package models

import (
	"fmt"
	"gini2p/config"
	"gini2p/logger"
	"gini2p/utility"
)

type ParkList struct {
	Id             int64  `json:"id"`
	ParkCode       int64  `json:"park_code"`
	ParkingName    string `json:"parking_name"`
	ParkingType    string `json:"parking_type"`
	Status         string `json:"status"`
	SpaceTotal     int    `json:"space_total"`
	SpaceEmpty     int    `json:"space_empty"`
	RecongnizeRate int    `json:"recongnize_rate"`
	UsedRate       string `json:"used_rate"`
	ArmVersion     string `json:"arm_version"`
}

type ParkInfoForm struct {
	Id               int64  `xorm:"pk autoincr BIGINT(64)"`
	Cid              int64  `xorm:"BIGINT(64)"`
	Status           int    `xorm:"not null default 0 TINYINT(4)"`
	ShowInApp        int    `xorm:"default 1 TINYINT(4)"`
	ArmVersion       string `xorm:"default '1' VARCHAR(10)"`
	ProvinceCode     int    `xorm:"not null default 0 INT(10)"`
	CityCode         int    `xorm:"not null default 0 INT(10)"`
	AreaCode         int    `xorm:"not null default 0 INT(12)"`
	RegionId         int    `xorm:"INT(11)"`
	ParkCode         int64  `xorm:"not null default 0 unique BIGINT(12)"`
	ParkCodeHash     string `xorm:"CHAR(33)"`
	ParkingName      string `xorm:"not null default '' VARCHAR(32)"`
	SpaceTotal       int    `xorm:"not null default 0 INT(12)"`
	SpaceEmpty       int    `xorm:"not null default 0 INT(12)"`
	TmpSpaceEmpty    int    `xorm:"not null default 0 INT(12)"`
	VipSpaceEmpty    int    `xorm:"not null default 0 INT(12)"`
	VipSpaceTotal    int    `xorm:"not null default 0 INT(12)"`
	TmpSpaceTotal    int    `xorm:"not null default 0 INT(12)"`
	ParkingAddress   string `xorm:"not null default '' VARCHAR(2048)"`
	DiscountInfo     string `xorm:"not null default '' VARCHAR(512)"`
	ParkingGps       string `xorm:"not null default '' VARCHAR(64)"`
	ContactUser      string `xorm:"not null default '' VARCHAR(32)"`
	ContactMobile    string `xorm:"not null default '' VARCHAR(12)"`
	ChargeCriterion  string `xorm:"not null default '' VARCHAR(2048)"`
	OpenTime         string `xorm:"not null default '' VARCHAR(12)"`
	ParkingType      string `xorm:"not null default '' VARCHAR(12)"`
	CostType         int    `xorm:"not null default 0 TINYINT(4)"`
	ParkingChannels  string `xorm:"not null default '' VARCHAR(2048)"`
	Others           string `xorm:"default '' VARCHAR(2048)"`
	UpdateTime       int64  `xorm:"not null default 0 BIGINT(11)"`
	SupportQuickPass int    `xorm:"not null default 0 TINYINT(4)"`
	SupportPayment   int    `xorm:"not null default 1 INT(8)"`
	Rentable         int    `xorm:"default 0 TINYINT(4)"`
	RecongnizeRate   int    `xorm:"default 0 TINYINT(4)"`
	SavePic          int    `xorm:"TINYINT(4)"`
	ParkingPrice     int    `xorm:"default 0 INT(4)"`
	ParkingPriceNoon int    `xorm:"default 0 INT(4)"`
}

//返回查询的数据,总数,总页数,error
func GetParkList(page, perpage int64, where string) ([]ParkList, int64, int64, error) {
	count, err := GetCount(where)
	if err != nil {
		return nil, 0, 0, nil
	}
	totalpage, offset := utility.MakePageInformation(count, page, perpage)
	parkinfos := make([]TParkInfo, 0)
	where = "status = 0 and parking_type != 2" + where
	if err := configEngine.Where(where).Limit(int(perpage), int(offset)).Find(&parkinfos); err != nil {
		logger.Error("app", fmt.Sprint("获取车场列表出错:", err))
		return nil, 0, 0, err
	}
	parklists := make([]ParkList, 0)
	for _, d := range parkinfos {
		parklist := &ParkList{}
		parklist.Id = d.Id
		parklist.ParkCode = d.ParkCode
		parklist.ParkingName = d.ParkingName
		if parktype, ok := config.ParkingType[d.ParkingType]; ok {
			parklist.ParkingType = parktype
		}
		if status, ok := config.ParkingStatus[d.Status]; ok {
			parklist.Status = status
		}
		parklist.SpaceTotal = d.SpaceTotal
		parklist.SpaceEmpty = d.SpaceEmpty
		parklist.RecongnizeRate = d.RecongnizeRate
		used := ((float64(d.SpaceTotal) - float64(d.SpaceEmpty)) / float64(d.SpaceTotal)) * 100
		parklist.UsedRate = fmt.Sprintf("%.2f", used)
		parklist.ArmVersion = d.ArmVersion
		parklists = append(parklists, *parklist)
	}
	return parklists, count, totalpage, nil
}

func GetCount(where string) (int64, error) {
	parkinfos := new(TParkInfo)
	where = "status = 0 and parking_type != 2" + where
	total, err := configEngine.Where(where).Count(parkinfos)
	if err != nil {
		logger.Error("sql", fmt.Sprint("查询parkinfo总数出错:", err))
		return 0, err
	}
	return total, nil
}

func GetAllPark(where string) ([]TParkInfo, error) {
	parkinfos := make([]TParkInfo, 0)
	where = "status = 0 and parking_type != 2" + where
	if err := configEngine.Where(where).Find(&parkinfos); err != nil {
		return parkinfos, err
	}
	return parkinfos, nil
}

func UpdateParkinfo(parkinfo *TParkInfo, id int64) error {
	_, err := configEngine.Where("id = ?", id).Update(parkinfo)
	if err != nil {
		logger.Error("app", fmt.Sprint("修改车场数据出错：", err))
		return err
	}
	return nil
}
